package com.zllkj.mm.wxapi

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.util.Log
import com.tencent.mm.opensdk.modelbase.BaseReq
import com.tencent.mm.opensdk.modelbase.BaseResp
import com.tencent.mm.opensdk.openapi.IWXAPI
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler
import com.tencent.mm.opensdk.openapi.WXAPIFactory
import com.zllkj.mm.BuildConfig
import com.zllkj.mm.MyApp
import com.zllkj.mm.common.L

class WXPayEntryActivity : Activity(), IWXAPIEventHandler {

    lateinit var mWxApi: IWXAPI

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mWxApi = WXAPIFactory.createWXAPI(this, MyApp.wxAppId, false)
        mWxApi.registerApp(MyApp.wxAppId)
        mWxApi.handleIntent(intent, this)
    }

    override fun onNewIntent(intent: Intent?) {
        super.onNewIntent(intent)
        setIntent(intent)
        mWxApi.handleIntent(intent, this)
    }

    override fun onReq(p0: BaseReq?) {

    }

    override fun onResp(baseResp: BaseResp?) {
        baseResp?.let {
            Log.d("acthome", "2222222222222222222222")
            if (baseResp.errCode == 0 ) {
                L.showToast("支付成功")
                sendBroadcast(Intent(SUCCESS_PAY))
                finish()
            } else if (baseResp.errCode != 0) {
                L.showToast("支付失败")
                sendBroadcast(Intent(FAILED_PAY))
                finish()
            }
        }
    }

    companion object{
        var SUCCESS_PAY = BuildConfig.APPLICATION_ID + this.javaClass.name + "SUCCESS_PAY"
        var FAILED_PAY = BuildConfig.APPLICATION_ID + this.javaClass.name + "FAILED_PAY"
    }
}